<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix=":" size="small" label-width="80px">
      <div class="flex wap flex-start">
        <el-form-item label="选择商品">
          <check-product :itemId="searchForm.productid" @checkedItem="checkedproduct" />
        </el-form-item>
        <el-form-item label="商品类型">
          <checked-cate :itemId="searchForm.producttypeid" @checkedItem="checkedcate" />
        </el-form-item>
        <el-form-item label="学员">
          <check-stu :itemId="searchForm.stuid" @checkedItem="checkedstu" />
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="searchForm.ispay" placeholder="请选择" clearable>
            <el-option label="未付款" :value="2"></el-option>
            <el-option label="已付款" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </div>
      <div class="flex wap flex-start">
        <el-form-item label="产品信息">
          <el-input v-model="searchForm.productinfo" placeholder="请输入产品信息" clearable></el-input>
        </el-form-item>
        <el-form-item label="付款信息">
          <el-input v-model="searchForm.payinfo" placeholder="请输入付款信息" clearable></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="dateTime" type="daterange" range-separator="~" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addItem">新增</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <el-table-column prop="stuname" label="学生"></el-table-column>
      <el-table-column prop="productinfo" label="产品信息" show-overflow-tooltip></el-table-column>
      <el-table-column prop="price" label="价格（元）"></el-table-column>
      <el-table-column prop="ispay" label="是否付款">
        <template slot-scope="scop">
          <el-tag type="success" v-if="scop.row.ispay == 1">已付款</el-tag>
          <el-tag type="warning" v-if="scop.row.ispay == 2">未付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="payimg" label="凭证">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="$Url + '/upload/' + scop.row.payimg"
            :preview-src-list="[$Url + '/upload/' + scop.row.payimg]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="payinfo" label="付款信息" show-overflow-tooltip></el-table-column>
      <el-table-column prop="paytime" label="付款时间"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.productsalelogid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="销售信息" center :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="addOreditForm" ref="addOrEditPro" :rules="rules" label-suffix=":" label-width="100px" class="edit-form">
        <el-form-item label="选择商品" prop="productid">
          <check-product :itemId="addOreditForm.productid" @checkedItemObj="addcheckedproduct" />
        </el-form-item>
        <el-form-item label="商品类型" prop="producttypeid">
          <checked-cate :itemId="addOreditForm.producttypeid" @checkedItem="addcheckedcate" />
        </el-form-item>
        <el-form-item label="学员" prop="stuid">
          <check-stu :itemId="addOreditForm.stuid" @checkedItem="addcheckedstu" />
        </el-form-item>
        <el-form-item label="产品信息" prop="productinfo">
          <el-input v-model="addOreditForm.productinfo" placeholder="请选择商品" disabled></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="addOreditForm.price" placeholder="请选择商品" disabled></el-input>
        </el-form-item>
        <div>
          <el-form-item label="是否付款" prop="ispay">
            <el-radio-group v-model="addOreditForm.ispay">
              <el-radio :label="2">未付款</el-radio>
              <el-radio :label="1">已付款</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="付款凭证" prop="payimg">
            <upload v-model="addOreditForm.payimg" />
          </el-form-item>
        </div>
        <el-form-item label="付款时间" prop="paytime">
          <el-date-picker v-model="addOreditForm.paytime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="付款信息" prop="payinfo">
          <el-input v-model="addOreditForm.payinfo" placeholder="请输入付款信息" clearable></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAddOrUpdata">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findProductSaleLog, addProductSaleLog, updateProductSaleLog, delProductSaleLog } from "@/api/user";
import checkProduct from "@/views/components/checkProduct"
import checkedCate from "@/views/components/checkedCate"
import checkStu from "@/views/components/checkStu"
import upload from "@/components/uploadFile";
export default {
  components: {
    checkProduct,
    checkedCate,
    checkStu,
    upload
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      checkItemIds: [],
      paymentids: [],
      dialogFormVisible: false,
      isEdit: false,
      dateTime: '',
      proName: '',
      proCate: '',
      searchForm: {
        productid: '',
        producttypeid: '',
        stuid: '',
        ispay: '',
        productinfo: '',
        payinfo: '',
        begintime: '',
        endtime: ''
      },
      addOreditForm: {
        productid: '',
        producttypeid: '',
        stuid: '',
        productinfo: '',
        price: '',
        ispay: 2,
        paytime: '',
        payinfo: '',
        payimg: ''
      },
      rules: {
        productid: [
          { required: true, message: '请选择商品', trigger: 'change' },
        ],
        producttypeid: [
          { required: true, message: '请选择商品分类', trigger: 'change' },
        ],
        stuid: [
          { required: true, message: '请选择学员', trigger: 'change' },
        ]
      }
    }
  },
  watch: {
    dateTime(n) {
      console.log(n);
      if (n) {
        this.searchForm.begintime = n[0]
        this.searchForm.endtime = n[1]
      } else {
        this.searchForm.begintime = ''
        this.searchForm.endtime = ''
      }
    }
  },
  computed: {
    queryData() {
      const { productid, producttypeid, stuid, ispay, productinfo, payinfo, begintime, endtime } = this.searchForm
      return {
        productid,
        producttypeid,
        stuid,
        ispay,
        productinfo,
        payinfo,
        begintime,
        endtime,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findProductSaleLog.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.productSaleLogList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    checkedproduct(val) {
      this.searchForm.productid = val
    },
    checkedcate(val) {
      this.searchForm.producttypeid = val
    },
    checkedstu(val) {
      this.searchForm.stuid = val
    },
    addcheckedproduct(val) {
      this.addOreditForm.productid = val.productid
      this.addOreditForm.price = val.newprice
      this.addOreditForm.productinfo = '【' + val.producttypename + '】' + val.productname
    },
    addcheckedcate(val) {
      this.addOreditForm.producttypeid = val
    },
    addcheckedstu(val) {
      this.addOreditForm.stuid = val
    },
    handleSelectionChange(val) {
    },
    addItem() {
      this.isEdit = false
      this.dialogFormVisible = true
      this.addOreditForm = {
        productid: '',
        producttypeid: '',
        stuid: '',
        productinfo: '',
        price: '',
        ispay: 2,
        paytime: '',
        payinfo: '',
        payimg: ''
      }
    },
    edit(row) {
      this.isEdit = true
      this.dialogFormVisible = true
      const {
        productsalelogid,
        productid,
        producttypeid,
        stuid,
        productinfo,
        price,
        ispay,
        paytime,
        payinfo,
        payimg
      } = row
      this.addOreditForm = {
        productsalelogid,
        productid,
        producttypeid,
        stuid,
        productinfo,
        price,
        ispay,
        paytime,
        payinfo,
        payimg
      }
    },
    saveAddOrUpdata() {
      this.$refs['addOrEditPro'].validate((valid) => {
        if (valid) {
          this.AddOrUpdata()
        } else {
          return false;
        }
      });
    },
    async AddOrUpdata() {
      if (this.isEdit) {
        const res = await updateProductSaleLog.post(this.addOreditForm);
        if (res.resultCode === "000000") {
          this.$message.success(res.resultInfo);
          this.dialogFormVisible = false
          this.getData()
        } else {
          this.$message.error(res.resultInfo);
        }
      } else {
        const res = await addProductSaleLog.post(this.addOreditForm);
        if (res.resultCode === "000000") {
          this.$message.success(res.resultInfo);
          this.dialogFormVisible = false
          this.getData()
        } else {
          this.$message.error(res.resultInfo);
        }
      }
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delProductSaleLog.post({ productsalelogid: id }).then(res => {
          if (res.resultCode === "000000") {
            this.$message({
              type: "success",
              message: "删除成功!"
            })
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
